// pages/fuyaoguanli/fuyaoguanli.js
const app = getApp()
const util = require('../../utils/util.js')
const MONTHS = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'June.', 'July.', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.'];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeNames: ['1'],
    date: util.formatDate(new Date()),
    year: new Date().getFullYear(), // 年份
    month: new Date().getMonth() + 1, // 月份
    day: new Date().getDate(),
    currentyear: new Date().getFullYear(), // 年份
    currentmonth: new Date().getMonth() + 1, // 月份
    currentday: new Date().getDate(),
    str: MONTHS[new Date().getMonth()], // 月份字符串
    demo4_days_style: [],
    startdate: util.formatDateMonth(new Date()),
    enddate: util.formatDateMonth(new Date()),
    drugdateList: [],
    drugmanListno: [],
    drugmanListyes: [],
    drugmanNum: 0,
    classid: null,
    activeqiandao: 0
  },
  onclasschange({
    detail
  }) {
    this.setData({
      classid: detail,
    });
    this.queryData(this.data.day);

  },
  onChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },
  xufuyaoClick: function (e) {
    var index = e.currentTarget.dataset.index;
    var obj = this.data.drugmanListno[index]
    var mess = JSON.stringify(obj)
    wx.navigateTo({
      url: '../fuyaotixingdetail/fuyaotixingdetail?mess=' + mess + "&type=1" + "&date=" + this.data.date
    })

  },
  yifuyaoClick: function (e) {
    var index = e.currentTarget.dataset.index;
    var obj = this.data.drugmanListyes[index]
    var mess = JSON.stringify(obj)
    wx.navigateTo({
      url: '../fuyaotixingdetail/fuyaotixingdetail?mess=' + mess + "&type=2" + "&date=" + this.data.date
    })

  },
  previewImg: function (e) {
    var index = e.currentTarget.dataset.index;
    console.log(index)
    var a = index.split(",")
    var url = a[1]
    var that = this;
    var imgArr = []
    for (var i = 0; i < that.data.drugList[a[0]].Attachments.length; i++) {
      imgArr.push(that.data.drugList[a[0]].Attachments[i].attachmentUrl)
    }
    wx.previewImage({
      current: url, //当前图片地址
      urls: imgArr, //所有要预览的图片的地址集合 数组形式
      success: function (res) {},
      fail: function (res) {},
      complete: function (res) {},
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var list = wx.getStorageSync('classlist');
    var classlist = []
    var classobj = {
      text: "全部班级",
      value: 0
    }
    classlist.push(classobj)
    for (var i = 0; i < list.length; i++) {
      var classobj = {
        text: list[i].className,
        value: list[i].classID
      }
      classlist.push(classobj)
    }
    this.setData({
      classid: classlist[0].value,
      classlist: classlist
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.queryData(this.data.day);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  next: function (event) {
    console.log(event.detail);
    this.setData({
      year: event.detail.currentYear,
      month: event.detail.currentMonth,
      day: 1,
      date: util.formatDate(new Date(event.detail.currentYear + "-" + util.formatNumber(event.detail.currentMonth) + "-01")),
    })
    this.queryData(this.data.day);

  },

  prev: function (event) {
    console.log(event.detail);
    this.setData({
      year: event.detail.currentYear,
      month: event.detail.currentMonth,
      day: 1,
      date: util.formatDate(new Date(event.detail.currentYear + "-" + util.formatNumber(event.detail.currentMonth) + "-01")),
    })
    this.queryData(this.data.day);
  },

  dateChange: function (event) {
    console.log(event.detail);
    this.setData({
      year: event.detail.currentYear,
      month: event.detail.currentMonth,
      day: 1,
      date: util.formatDate(new Date(event.detail.currentYear + "-" + util.formatNumber(event.detail.currentMonth) + "-01")),
    })
    this.queryData(this.data.day);
  },

  dayClick: function (event) {
    console.log(event.detail);
    this.setData({
      year: event.detail.year,
      month: event.detail.month,
      day: event.detail.day,
      date: util.formatDate(new Date(event.detail.year + "-" + util.formatNumber(event.detail.month) + "-" + util.formatNumber(event.detail.day))),
    })
    this.queryData(this.data.day);
  },


  setDateColor: function (that, day) {
    const days_count = new Date(that.data.year, that.data.month, 0).getDate();
    let demo4_days_style = new Array;
    for (let i = 1; i <= days_count; i++) {
      var flag = 0;

      if (that.data.drugdateList != null) {
        for (let j = 0; j < that.data.drugdateList.length; j++) {
          var a = that.data.drugdateList[j].date;
          var days = parseInt(a.substring(8, 10));

          if (i == days) {
            flag = 1
            demo4_days_style.push({
              month: 'current',
              day: i,
              color: 'white',
              background: '#27c868'
            });
            break;
          }
        }
      }

      if (flag == 0) {
        if (i == this.data.currentday && this.data.year == this.data.currentyear && this.data.month == this.data.currentmonth) {
          demo4_days_style.push({
            month: 'current',
            day: i,
            color: '#27c868',
            background: 'white'

          });
        }
      }
    }
    demo4_days_style.push({
      month: 'current',
      day: day,
      color: 'black',
      background: 'yellow'

    });
    that.setData({
      demo4_days_style: demo4_days_style
    });
  },
  queryData: function (day) {
    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "classid": this.data.classid,
      "schoolid": app.globalData.schoolId,
      "date": this.data.date,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileType": app.globalData.mobileType,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_drug"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {

          that.setData({
            drugmanListno: res.data.drugmanListno,
            drugdateList: res.data.drugdateList,
            drugmanListyes: res.data.drugmanListyes,
            drugmanNum: res.data.drugmanNum,
            startdate: util.formatDateMonth(new Date(res.data.leftDay.substring(0, 7))),
          })
          that.setDateColor(that, day);
          //数据清除
        } else {
          wx.showToast({
            title: "获取数据失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })



        }
      },
      fail: function (res) {

        wx.showToast({
          title: "获取数据失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })


      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },
})